package com.example.demonstrate.wang.controller;

import com.example.demonstrate.wang.model.TaskStatus;
import org.springframework.messaging.handler.annotation.MessageMapping;
import org.springframework.messaging.handler.annotation.SendTo;
import org.springframework.messaging.simp.config.MessageBrokerRegistry;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker;
import org.springframework.web.socket.config.annotation.StompEndpointRegistry;
import org.springframework.web.socket.config.annotation.WebSocketMessageBrokerConfigurer;

/**
 * @Author along
 * @Date 2024/6/12 10:41
 */

@Controller
@EnableWebSocketMessageBroker
public class WebSocketController implements WebSocketMessageBrokerConfigurer {

    @Override
    public void configureMessageBroker(MessageBrokerRegistry config) {
        config.enableSimpleBroker("/topic");
        config.setApplicationDestinationPrefixes("/app");
    }
    @Override
    public void registerStompEndpoints(StompEndpointRegistry registry) {
        registry.addEndpoint("/ws").withSockJS();
    }

    @MessageMapping
    @SendTo("/topic/task/status")
    public TaskStatus updateTaskStatus(TaskStatus status) {
        // 更新任务状态
        return status;
    }

    // test websocket connection
    @GetMapping("/test/socket")
    public String testWebSocket() {
        return "Connected to WebSocket";
    }
}
